<!--
 * @Description: 码表下拉框
 * @Author: your name
 * @Date: 2023-09-12
 * @LastEditTime: 2023-09-12
 * @LastEditors: Please set LastEditors
-->
<template>
  <el-select
    v-model="val"
    @change="onChange"
    v-bind="$attrs"
    v-on="$listeners"
    :placeholder="placeholder"
    class="w100"
  >
    <el-option label="全部" value="" v-if="isAll" />
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.name"
      :value="item.value"
    >
    </el-option>
  </el-select>
</template>
<script>
// import { dataDictionaryGet } from '@/api';

export default {
  name: 'SelectCode',
  props: {
    // 数值
    value: {
      type: String | Array,
      default: ''
    },
    // 接口请求参数值
    code: {
      type: String | Array,
      default: ''
    },
    isAll: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data() {
    return {
      val: '',
      options: []
    };
  },
  watch: {
    code: {
      immediate: true,
      handler() {
        // 根据数据查询码表并赋值给options
        if (Array.isArray(this.code)) {
          this.options = this.code;
        } else {
          this.getInitCode();
        }
      }
    },
    value() {
      this.val = this.value;
    }
  },
  methods: {
    getInitCode() {
      // dataDictionaryGet({ codeType: this.code })
      //   .then(res => {
      //     this.options = res.data;
      //     this.val = this.value;
      //   })
      //   .catch(err => {
      //     console.log('请求下拉框码值error：', err);
      //   });
    },
    onChange(v) {
      this.$emit('input', v);
    }
  }
};
</script>
